package cn.tedu.submarine;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.im.InputContext;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Random;
import java.util.Arrays;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
/** 整个游戏世界 */
public class World extends JPanel{
    public static final int   WIDTH=641;
    public static final  int  HEIGHT=479;

    public static final int RUNNING=0;
    public static final int GAME_OVER=1;
    private int state = RUNNING;
    private Battleship ship=new Battleship();
    private SeaObject [] submarines={};
    private Mine[]mines={};
    private Bomb[]bombs={};

    /** 获取图片 */
    public void paint(Graphics g){
        switch (state) {
            case GAME_OVER:
                Images.gameover.paintIcon(null, g, 0, 0);
                break;
            case RUNNING:
                Images.sea.paintIcon(null, g, 0, 0);


                ship.paintImage(g);
                for (int i = 0; i < submarines.length; i++) {
                    submarines[i].paintImage(g);
                }
                for (int i = 0; i < mines.length; i++) {
                    mines[i].paintImage(g);
                }
                for (int i = 0; i < bombs.length; i++) {
                    bombs[i].paintImage(g);
                }
        }
        g.drawString("SCORE："+score,200,50);
        g.drawString("LIFE："+ship.getLife(),400,50);
    }

    /** 生成潜艇组 */
    public SeaObject nextSubmarine(){
            Random rand=new Random();
            int type=rand.nextInt(20);
            if (type<10){
                return new TorpedoSubmarine();
            }else if(type<15){
                return new ObserveSubmarine();
            }else{
                return new MineSubmarine();
            }
    }

    public int subEnterIndex=0;
    /** 潜艇入场*/
    public void submarinesEnterAction(){
        subEnterIndex++;
        if (subEnterIndex%40==0){
            SeaObject obj = nextSubmarine();
            submarines=Arrays.copyOf(submarines,submarines.length+1);
            submarines[submarines.length-1]=obj;
        }
    }
    private  int mineEnterIndex=0;
    /** 周五水雷入场 */
    public  void mineEnterAction(){
        mineEnterIndex++;
        if (mineEnterIndex%100==0){
            for (int i = 0; i<submarines.length;i++){
                if (submarines[i] instanceof MineSubmarine){
                    MineSubmarine ms=(MineSubmarine) submarines[i];
                    Mine obj = ms.shootMine();
                    mines = Arrays.copyOf(mines,mines.length+1);
                    mines[mines.length-1] = obj;
                }
            }
        }
    }


    public void moveAction(){
        for (int i= 0;i<submarines.length;i++){
            submarines[i].move();
        }
        for (int i= 0;i<mines.length;i++){
            mines[i].move();
        }
        for (int i= 0;i<bombs.length;i++){
            bombs[i].move();
        }
    }

    public void outOfBoundsAction() {
        for (int i = 0; i < submarines.length; i++) {
            if (submarines[i].isOutOfBounds() || submarines[i].isDead()) {
                submarines[i] = submarines[submarines.length - 1];
                submarines = Arrays.copyOf(submarines, submarines.length - 1);
            }
        }
        for (int i = 0; i < bombs.length; i++) {
            if (bombs[i].isOutOfBounds() || bombs[i].isDead()) {
                bombs[i] = bombs[bombs.length - 1];
                bombs = Arrays.copyOf(bombs, bombs.length - 1);
            }
        }
        for (int i = 0;i<mines.length;i++){
            if (mines[i].isOutOfBounds() || mines[i].isDead()){
                mines[i]=mines[mines.length-1];
                mines=Arrays.copyOf(mines,mines.length-1);
            }
        }
    }

    private int score = 0;//玩家得分
    /** 深水炸弹与潜艇的碰撞 */
    public void bombBangAction(){
        for (int i = 0;i<bombs.length;i++){
            Bomb b =bombs[i];
            for (int j = 0;j<submarines.length;j++){
                SeaObject s = submarines[j];
                if (b.isLive() && s.isLive() && s.isHit(b)){
                    s.goDead();
                    b.goDead();

                    if (s instanceof EnemyScore){//若被撞潜艇为分
                        EnemyScore es = (EnemyScore) s;
                        score += es.getScore();
                    }
                    if (s instanceof EnemyLife){//若被撞潜艇为命
                        EnemyLife el = (EnemyLife) s;
                        int num = el.getLife();
                        ship.addLife(num);
                    }
                }
            }
        }
    }

    public void mineBangAction(){
        for (int i=0;i<mines.length;i++){
            Mine m = mines[i];
            if (m.isLive() && ship.isLive() && m.isHit(ship)){
                m.goDead();
                ship.subtractLife();
            }
        }
    }

    public void checkGameOverAction(){
        if (ship.getLife()<=0){
            state=GAME_OVER;
        }
    }
        /** 启动程序的执行 */
    public void action(){
       KeyAdapter k= new KeyAdapter() {

           public void keyPressed(KeyEvent e) {//当按键弹起时会自动执行-----不要求掌握
                if (e.getKeyCode()== KeyEvent.VK_SPACE){
                    Bomb obj=ship.shootBomb();
                    bombs=Arrays.copyOf(bombs,bombs.length+1);
                    bombs[bombs.length-1]=obj;
                }
                if (e.getKeyCode()==KeyEvent.VK_LEFT){//若按键左箭头--不要求掌握
                    ship.moveLeft();
                }
                if(e.getKeyCode()==KeyEvent.VK_RIGHT){
                    ship.moveRight();
                }
           }
       };
       this.addKeyListener(k);
        Timer timer=new Timer();
        int interval=10;//定时间隔（以毫秒为单位)
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
            submarinesEnterAction();
            mineEnterAction();
            moveAction();
            outOfBoundsAction();
            bombBangAction();
            mineBangAction();
            checkGameOverAction();
            repaint();
            if (ship.getLife()==0){
                timer.cancel();
            }
            }
        }, interval, interval);
    }
    public static void main(String[] args) {

        JFrame frame = new JFrame();
        World world=new World();
        world.setFocusable(true);
        frame.add(world);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH+16,HEIGHT+39);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        world.action();

    }
}
